package com.yanqu.road.entity.activity.rankunionwar.config;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.rankunionwar.eRankUnionWarDivisionType;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RankUnionWarConfig {

    private ActivityInfo activityInfo;

    private List<ActivityConditionInfo> activityConditionInfoList;

    private Map<Integer, RankUnionWarTimeConfig> timeConfigMap;

    private Map<Integer, RankUnionWarDivisionConfig> divisionConfigMap;

    private Map<Integer, RankUnionWarFloorConfig> floorConfigMap;

    //宝箱九宫格
    private int boxLength = 9;
    private int turnBattleStartSec;
    private int turnBattleWait;
    private int battleOccCount;
    private int scoreItemId;
    private int unionCount;
    private int guwuMax;
    private List<Integer> guwuCostList;
    private int guwuAdd;
    private int gambleCount;
    private int gambleRankTop;
    private int gameLoseCount;
    private int patronsWinDebuff;
    private int patronsWinMaxCount;
    private String lookReward;
    private int battleInterval;
    private List<Integer> patronsCostList;
    private int recruitItemId;
    private int floorItemId;
    private int waitWinTime;//占领直接胜利时间

    private int maxPatrons;//每一个职业前多少个门客参加比赛

    private int killNumNotice;//杀了多少个以上有战报

    private List<String> inspireRewardList;//鼓舞奖励
    private List<Integer> inspireRewardWeightList;//鼓舞奖励权重

    private List<String> donateRewardList;//x奖励
    private List<Integer> donateRewardWeightList;//奖励权重

    private int bigBossNum;//种子选手个数

    private int lineWinAddScore;//每条线胜利加的附加分

    private int battleNoSameTurn;//大于这个轮次才会碰到一起打

    //赛区对应商会对战个数
    private Map<Integer,Integer> divisionNumMap;

    //开战后多少时间后的碰撞时间变化
    private Map<Long,Long> battleTimeMap;

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public List<ActivityConditionInfo> getActivityConditionInfoList() {
        return activityConditionInfoList;
    }

    public List<Integer> getGamebleRightCountList(int turn) {
        List<Integer> res  =  new ArrayList<>();
        for(ActivityConditionInfo conditionInfo : activityConditionInfoList){
            if(conditionInfo.getType() == eGamePlayerEventType.RankUnionWarMatchGuess.getValue()){
                //答对了多少场
                if(turn == conditionInfo.getParamList().get(2).intValue()){
                    res.add(conditionInfo.getParamList().get(3).intValue());
                }
            }
        }
        return res;
    }


    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public void setActivityConditionInfoList(List<ActivityConditionInfo> activityConditionInfoList) {
        this.activityConditionInfoList = activityConditionInfoList;
    }

    public void initConfig(Map<String, ActivityConfig> activityConfigMap, Map<Integer, RankUnionWarTimeConfig> tempTimeConfigMap, Map<Integer, RankUnionWarDivisionConfig> tempDivisionConfigMap, Map<Integer, RankUnionWarFloorConfig> tempFloorConfigMap) {
        bigBossNum = 8;
        //配置删掉了
//        bigBossNum = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_MATCH_BIGBOSS_NUM.getName()).getIntValue();

        lineWinAddScore = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_BATTLTE_ADD_SCORE.getName()).getIntValue();
        battleNoSameTurn = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_BATTLTE_ADD_SPACE.getName()).getIntValue();
        battleOccCount = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_TURN_BATTLE_OCC_COUNT.getName()).getIntValue();
        turnBattleWait = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_TURN_BATTLE_WAIT.getName()).getIntValue();
        turnBattleStartSec = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_TURN_START_BATTLE.getName()).getIntValue();
        scoreItemId = activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue();
        waitWinTime = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_BATTLE_WAIT_WIN.getName()).getIntValue();
        unionCount = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_UNION_COUNT.getName()).getIntValue();
        guwuMax = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_GUWU_MAX.getName()).getIntValue();
        guwuCostList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_GUWU_COST.getName()).getValue(),"\\|");
        guwuAdd = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_GUWU_ADD.getName()).getIntValue();
        gambleCount = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_GAMBLE_COUNT.getName()).getIntValue();
        gambleRankTop = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_GAMBLE_RANK_TOP.getName()).getIntValue();
        gameLoseCount = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_LOSE_COUNT.getName()).getIntValue();
        patronsWinDebuff = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_PATRONS_WIN_DEBUFF.getName()).getIntValue();
        patronsWinMaxCount = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_PATRONS_WIN_MAX_COUNT.getName()).getIntValue();
        lookReward = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_LOOK_REWARD.getName()).getValue();
        battleInterval = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_BATTLE_INTERVAL.getName()).getIntValue();
        patronsCostList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_PATRONS_COST.getName()).getValue(),"\\|");
        recruitItemId = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_PATRONS_RECRUIT_ITEM_ID.getName()).getIntValue();
        floorItemId = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_FLOOR_ITEM_ID.getName()).getIntValue();
        maxPatrons = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_MAX_PATRONS.getName()).getIntValue();
        String INSPIRE_REWARD = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_INSPIRE_REWARD.getName()).getValue();
        List<String> rewardList = StringUtils.stringToStringList(INSPIRE_REWARD,"\\|");
        inspireRewardList = new ArrayList<>();
        inspireRewardWeightList = new ArrayList<>();
        for(String rewardWeightStr : rewardList){
            List<String> tempRewardWeightList = StringUtils.stringToStringList(rewardWeightStr,";");
            inspireRewardList.add(tempRewardWeightList.get(0));
            inspireRewardWeightList.add(Integer.valueOf(tempRewardWeightList.get(1)));
        }
        killNumNotice = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_PATRON_KILL_NUM_NOTICE.getName()).getIntValue();

        String donateRewardStr = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_DONATE_REWARD.getName()).getValue();
        donateRewardList = new ArrayList<>();
        donateRewardWeightList = new ArrayList<>();
        List<String> tempDonateRewardList = StringUtils.stringToStringList(donateRewardStr,"\\|");
        for(String donateReward : tempDonateRewardList){
            List<String> tempeRewardWeightList = StringUtils.stringToStringList(donateReward,";");
            donateRewardList.add(tempeRewardWeightList.get(0));
            donateRewardWeightList.add(Integer.valueOf(tempeRewardWeightList.get(1)));
        }

        for(RankUnionWarTimeConfig timeConfig : tempTimeConfigMap.values()){
            timeConfig.setStartTime(timeConfig.getBeginTime() * DateHelper.MINUTE_MILLIONS + activityInfo.getBeginTime() * 1000);
            timeConfig.setEndTime((timeConfig.getBeginTime() + timeConfig.getDurTime()) * DateHelper.MINUTE_MILLIONS + activityInfo.getBeginTime() * 1000);
            timeConfig.setTurnWaitTime(turnBattleStartSec * 1000);
        }

        String RANKUNIONWAR_BEGIN_MATCH = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_BEGIN_MATCH.getName()).getValue();
        List<String> divisionList = StringUtils.stringToStringList(RANKUNIONWAR_BEGIN_MATCH,";");
        divisionNumMap = new ConcurrentHashMap<>();
        for(String divisionNumStr : divisionList){
            List<Integer> divisionNumList = StringUtils.stringToIntegerList(divisionNumStr,"=");
            divisionNumMap.put(divisionNumList.get(0),divisionNumList.get(1));
        }

        String RANKUNIONWAR_BATTLTE_TIME = activityConfigMap.get(eActivityConfigType.RANKUNIONWAR_BATTLTE_TIME.getName()).getValue();
        List<String> battleTimeStrList = StringUtils.stringToStringList(RANKUNIONWAR_BATTLTE_TIME,"\\|");
        battleTimeMap = new ConcurrentHashMap<>();
        for(String battleTimeStr : battleTimeStrList){
            List<Long> battleTimeList = StringUtils.stringToLongList(battleTimeStr,";");
            battleTimeMap.put(battleTimeList.get(0),battleTimeList.get(1));
        }

        timeConfigMap = tempTimeConfigMap;
        divisionConfigMap = tempDivisionConfigMap;
        floorConfigMap = tempFloorConfigMap;
    }

    public int getLineWinAddScore() {
        return lineWinAddScore;
    }

    public int getBattleNoSameTurn() {
        return battleNoSameTurn;
    }

    public Map<Integer, Integer> getDivisionNumMap() {
        return divisionNumMap;
    }

    public Map<Long, Long> getBattleTimeMap() {
        return battleTimeMap;
    }

    public int getBigBossNum() {
        return bigBossNum;
    }

    public int getBattleOccCount() {
        return battleOccCount;
    }

    public int getTurnBattleWait() {
        return turnBattleWait;
    }

    //每一轮比赛开始前的等待
    public int getTurnBattleStartSec() {
        return turnBattleStartSec;
    }

    public int getGuwuAdd(){
        return guwuAdd;
    }

    public int getGambleRankTop() {
        return gambleRankTop;
    }

    public int getBoxLength() {
        return boxLength;
    }

    public int getKillNumNotice() {
        return killNumNotice;
    }

    public int getMaxPatrons() {
        return maxPatrons;
    }

    public int getGameLoseCount() {
        return gameLoseCount;
    }

    public int getWaitWinTime() {
        return waitWinTime;
    }

    public int getPatronsWinDebuff() {
        return patronsWinDebuff;
    }

    public int getPatronsWinMaxCount() {
        return patronsWinMaxCount;
    }

    public int getBattleInterval() {
        return battleInterval;
    }

    //获取不同时段的对应的战斗时间间隔
    public long getBattleIntervalTime(int turn,long getTime) {
        long battleInterval = 0;
        if(timeConfigMap.containsKey(turn)){
            RankUnionWarTimeConfig timeConfig = timeConfigMap.get(turn);
            if(getTime < timeConfig.getStartTime() || getTime > timeConfig.getEndTime()){
                return battleInterval;
            }
            List<Long> passTimeList = new ArrayList<>(battleTimeMap.keySet());
            Collections.sort(passTimeList);
            for(long passTime : passTimeList){
                if(getTime < timeConfig.getStartTime() + passTime){
                    return battleTimeMap.get(passTime);
                }
            }
            //配错了，直接返回最后的时间
            return battleTimeMap.get(passTimeList.get(passTimeList.size() - 1));
        }
        return battleInterval;
    }



    public int getGambleCount() {
        return gambleCount;
    }

    public String getLookReward() {
        return lookReward;
    }

    public int getRecruitItemId() {
        return recruitItemId;
    }

    public int getScoreItemId() {
        return scoreItemId;
    }

    public int getFloorItemId() {
        return floorItemId;
    }

    public Map<Integer, RankUnionWarTimeConfig> getTimeConfigMap() {
        return timeConfigMap;
    }

    public Map<Integer, RankUnionWarDivisionConfig> getDivisionConfigMap() {
        return divisionConfigMap;
    }

    public RankUnionWarDivisionConfig getDivisionConfig(int division) {
        return divisionConfigMap.get(division);
    }

    public Map<Integer, RankUnionWarFloorConfig> getFloorConfigMap() {
        return floorConfigMap;
    }

    public int getUnionCount() {
        return unionCount;
    }

    public List<String> getInspireRewardList() {
        return inspireRewardList;
    }

    public List<Integer> getInspireRewardWeightList() {
        return inspireRewardWeightList;
    }

    public List<String> getDonateRewardList() {
        return donateRewardList;
    }

    public void setDonateRewardList(List<String> donateRewardList) {
        this.donateRewardList = donateRewardList;
    }

    public List<Integer> getDonateRewardWeightList() {
        return donateRewardWeightList;
    }

    public void setDonateRewardWeightList(List<Integer> donateRewardWeightList) {
        this.donateRewardWeightList = donateRewardWeightList;
    }

    /*下面都是数据处理的*/

    //获取当前
    public RankUnionWarTimeConfig getTimeConfig(long nowTime){
        if(activityInfo == null){
            return null;
        }
        if(nowTime /1000 < activityInfo.getBeginTime()){
            return null;
        }
        RankUnionWarTimeConfig firstConfig = timeConfigMap.get(1);
        if(nowTime /1000 >= activityInfo.getBeginTime() && nowTime < firstConfig.getEndTime()){
            return firstConfig;
        }
        for(int turn = 2 ; timeConfigMap.containsKey(turn); turn++){
            RankUnionWarTimeConfig timeConfig = timeConfigMap.get(turn);
            if(nowTime < timeConfig.getEndTime()){
                return timeConfig;
            }
        }
        return timeConfigMap.get(timeConfigMap.size()-1);
    }

    public RankUnionWarTimeConfig getTimeConfig(int turn){
        return timeConfigMap.get(turn);
    }

    public RankUnionWarTimeConfig getNextTimeConfig(int turn){
        return timeConfigMap.get(turn + 1);
    }

    public RankUnionWarTimeConfig getLastTimeConfig(){
        int maxTurn = 0;
        for(int turn : timeConfigMap.keySet()){
            if(turn > maxTurn){
                maxTurn = turn;
            }
        }
        return timeConfigMap.get(maxTurn);
    }

    //获取时间是在第几轮了
    public int getGameTurn(long nowTime){
        for(RankUnionWarTimeConfig timeConfig : timeConfigMap.values()){
            if(nowTime > timeConfig.getStartBattleTime() && nowTime < timeConfig.getEndTime()){
                return timeConfig.getSession();
            }
        }
        return 0;
    }

    //看看是不是完全随机的赛区，最后面四个赛区对打是完全随机
    public boolean isAllRandomDivision(int divisionId){
        if(divisionId >= eRankUnionWarDivisionType.Zuanshi.getValue()){
            return true;
        }
        return false;
    }

    //获得下一个战区id,已经最大了返回自己
    public int getNextDivision(int divisionId){
        List<Integer> divisionIdList = new ArrayList<>(divisionConfigMap.keySet());
        if(divisionIdList.contains(divisionId + 1)){
            return divisionId + 1;
        }
        return divisionId;
    }

    public int getPreviousDivision(int divisionId){
        List<Integer> divisionIdList = new ArrayList<>(divisionConfigMap.keySet());
        if(divisionIdList.contains(divisionId - 1)){
            return divisionId - 1;
        }
        return divisionId;
    }

    //获得当前的最大战区
    public int getMaxDivision(List<Integer> divisionIdMap){
        Collections.sort(divisionIdMap);
        return divisionIdMap.get(divisionIdMap.size() -1);
    }

    //判断时间是不是战斗时间
    public boolean isBattleTime(long time){
        for(RankUnionWarTimeConfig timeConfig : timeConfigMap.values()){
            if(time > timeConfig.getStartBattleTime() && time < timeConfig.getEndTime()){
                return true;
            }
        }
        return false;
    }

    //买门客消耗
    public int getPatronsBuyTimeCost(int buyTime){
        if(buyTime > patronsCostList.size() || buyTime < 1){
            return Integer.MAX_VALUE;
        }
        return patronsCostList.get(buyTime - 1);
    }

    public int getPatronsBuyMaxTimes(){
        return patronsCostList.size();
    }

    //鼓舞次数对应消耗
    public int getInspireCost(int times){
        if(times >= guwuCostList.size()){
            return guwuCostList.get(guwuCostList.size() - 1);
        }
        return guwuCostList.get(times);
    }

}
